t
Breakpoints and the Stack Window

*107*This portion of the tutorial introduces the*998* Stack window and the various*327* Exec *609*menu items that help you control and examine Prograph classes and methods while your application is running. The Prograph editor and interpreter are designed to work together during application development. A variety of runtime facilities help you examine and edit all aspects of your application as it runs.

u
Delete the test method from the Universal Methods window.

u
Selectively load the quickSort universal method from the Algorithms.pgs file in the Tutorial Examples ƒ folder.  Select*667* *1080*Tidy Icons *1175*from the*1184* Windows menu to bring the quickSort method into view, if necessary.

u
Open the quickSort method and inspect its two cases.

The quickSort expects a list as input, which it sorts using a list-partitioning multiplex and recursive reference to itself in its second case. When done, quickSort passes the sorted list out its output. While its second case may not make sense to you now, using the Prograph Stack window and breakpoints can help you to understand quickly how this method works.

u
Bring the case window quickSort 1:2 topmost and active. Click to select the Match operation, which is checking for an empty-list condition. Select*112* *330*Breakpoint On *611*from the Exec menu. 

___________________________________________________________
NOTE:  The Debug menu item in the Exec menu must be on (checked) for breakpoints and step/show mode to be enabled.
-----------------------------------------------------------

u
Command-click the close box to close both case windows of the quickSort method.

u
Create a universal method, Do Sort, which looks like this:

u
Close the Do Sort 1:1 case window and, while the Do Sort method icon is selected in the now-active Universal window, select*339* *981*Set Program *620* from the Exec menu. This lets you execute and stop executing the Do Sort method with a simple Command-R. If Set Program is not used, you have to select the Do Sort icon in the Universal window (or alternatively bring the Do Sort 1:1 case window to the front) and select the Execute Method item in the Exec menu each time you want to do the sort.

u
Select*666* *996*Stack from the*663* *1185*Windows menu. The tall, narrow Stack window opens to the right of the Universal and Classes windows.

===========================================================
TIP:  You can move these and any other Prograph windows around to your*1098* liking and select the*943* Remember Windows *1187*item under the*665* Windows menu to have Prograph maintain your own preferred window configuration.
===========================================================

The*1188* Stack window title has an icon that looks like a stack of plates. Its window pane is also shaded with the same gray pattern found in a*1084* method-execution window. These visual cues identify the*999* Stack window as one of the windows in which Prograph lets you inspect, but not edit, methods during execution. 

The icons displayed in the Stack and execution windows are not directly editable. Double-clicking method icons in the Stack window opens their respective execution case windows. Double-clicking the background and roots or terminals in an execution case window opens editable case or Value windows, respectively. If the double-clicked elements are not editable, such as primitives, the ˙Info… dialog is opened to the appropriate category, with the clicked item selected in the scroll list. To see the Stack window in action:

u
Press Command-R to run Do Sort. The Classes and Universal windows close, and the Stack window displays an icon.

At first glance, Stack window method icons look like icons in the Universal Methods window or those in class Methods windows. On closer inspection, you see the addition of a*1002* case-number reference in the center of the icon. The Stack window excerpt above indicates that case #1 of the Do Sort method is executing. Since it is the first icon to be displayed under the fuzzy gray line*1003* that appears at the top of the Stack window indicating current execution, you know that Do Sort is the first method to execute upon selecting Run Do Sort from the Exec menu.

This first entry in the Stack window is almost immediately obscured by the Enter list:… dialog that results from execution of the Do Sort operations:

u
Type (3 8 2 6) in response to the dialog:

u
Press Return to dismiss the dialog. The Stack window updates. Case #1 of quickSort is executing, which results in the execution flow being interrupted by the Breakpoint you set on the Match operation. The Stack window now displays a second method icon, indicating that the first case of quickSort is executing.

u
Press Return. The match on the empty-list condition fails, and quickSort calls its second case. The quickSort 1:1 execution window closes, the quickSort 2:2 window opens, and the Stack window’s quickSort method icon is updated to reflect the execution of case #2.

u
Press Return one more time.

u
To see the relationship between the Do Sort and quickSort execution icons in the Stack window, activate the Stack window and double-click the Do Sort icon. An execution window opens on Do Sort 1:1 and indicates that the ask operation has received its user response and has passed its output to a quickSort operation.

u
Double-click on the terminal of the quickSort operation in the Do Sort execution window to open a Value window that confirms that the list, (3 8 2 6), is flowing into quickSort. Dismiss the Value window and the Do Sort 1:1 execution window.

___________________________________________________________
NOTE:  The*1100* Stack window provides a powerful overview of what is happening in your program. The ability to open execution windows on methods that are pending completion gives you a closer look at how your application got to its present state.

Open an execution window on a still-not-completed method by double-clicking its icon in the Stack window. The highlighted icon in the method execution window is the operation calling the method next down in the Stack window. The terminal of the highlighted operation displays a hollow circle, indicating that data has arrived to that point. The output roots of the highlighted operation remain gray, indicating that no output has yet resulted.
-----------------------------------------------------------

u
Continue pressing Return to step deeper and deeper into the quickSort recursive algorithm. Activity pops back and forth between the execution windows of the first and second case of quickSort, revealing the progress of the sort. Each change in the selection of the active execution window coincides with an update of the Stack window. At its deepest level of recursion during this example, the Stack window looks like this.

u
Continue pressing Return. Once it has “bottomed out,” the recursion starts to unravel itself, resolving pending executions and joining sorted sublist to sublist. When the first call to quickSort joins the last two sorted sublists, its execution is finished. The fully sorted list is output from the quickSort operation in the Do Sort method. Do Sort is completed, and the show operation confirms the result:

u
Dismiss the results dialog. The Do Sort method icon in the Stack window disappears. The Stack window is now empty, indicating that this mini-application has completed execution. 
Close the Stack window if it is still open.

Here the*108* Breakpoint you set in quickSort forced the interpreter into the stepping process, which allowed you to inspect the sorting algorithm as it proceeded. You were ready for the process with the Stack window open and the Breakpoint set. But what about the more typical scenario in which you decide to inspect program execution during runtime?

u
Open the quickSort 1:2 case window and select the Match operation. Select Breakpoint Off from the Exec menu.

u
Select Run Do Sort from the Exec menu and at the Enter list: prompt type a reasonably large list, such as:

 (45 3 7 3 8 6 9 2 21 64 3 6 7 34 2 1 21 27 13 33 1 5 56 28 2 4 7)

 and press Return.

u
Quickly issue the Prograph Break*104* command by pressing Command-. (Command and the period key) to interrupt execution. The Stack window opens together with the execution window of the method currently in the midst of executing.

You can inspect the execution window as before. If you activate the Stack window, you can scroll up and down the list and peek into the still pending executions of the second case of quickSort.

u
Press Return. Execution resumes with the Stack window still open. This slows execution down considerably because of all the window-animation updating. But watching the Stack window activity reveals a great deal about how your methods and their cases relate and call upon each other. You can issue the Break command (Command-.) any time to interrupt your application and open the execution window for the currently executing method.

The list of pending method executions in the Stack window keeps growing as sublists are partitioned. Eventually, the sort algorithm “bottoms out” and the sublists are put back together. As second cases of quickSort are resolved, they disappear from the bottom of the Stack window list until all are gone and the show… dialog pops up displaying the sorted long list. Only the Do Sort method is pending execution completion in the Stack window. 

u
Dismiss the sorted-list dialog. Do Sort finishes execution, and its method icon is removed from the Stack window. Prograph returns to the editor with whatever windows were open when you ran Do Sort. Close the Stack window if it is still open. 

The Stack window is one of the most useful interpreter facilities for learning what a Prograph application is doing as it runs. The Stack window can be opened any time you suspend execution of an application. So far, you have suspended execution with a*109* Breakpoint and Break command. As you explore interpreter features, notice that some automatically open the Stack window. Don’t hesitate to investigate opening the Stack window on your own as you practice techniques for interrupting and inspecting Prograph method execution in the next section of the tutorial.